package ooo.reindeer.cedf;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import ooo.reindeer.commons.utils.SerialNumberUtil;
import ooo.reindeer.logging.ILogger;
import ooo.reindeer.logging.LogUtil;


public class EventJack<V> {

    private static final ILogger logger = LogUtil.getLogger(EventJack.class);
    private static ObjectMapper mapper = new ObjectMapper();

    private String id;
    private String type;

    private Object data;

    public EventJack(String type) {
        this(type, null);
    }

    public EventJack(String type, Object data) {
        this.id = SerialNumberUtil.getSerialNumber();
        this.type = type;
        this.data = data;

    }

    public String getId() {
        return id;
    }

    public String getType() {
        return type;
    }

    public <T> T getData() {
        return (T) data;
    }

    public String toString() {

        try {
            return mapper.writeValueAsString(this);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public EventJack clone() {

        try {
            return mapper.readValue(this.toString(), EventJack.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static EventJack fromString(String json) {

        try {
            return mapper.readValue(json, EventJack.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @JsonCreator
    private EventJack(@JsonProperty("id") String id, @JsonProperty("type") String type, @JsonProperty("data") Object data) {
        this.id = id;
        this.type = type;
        this.data = data;
    }


}
